// pages/message/index.js
const $api = require("../../api/message").API

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    msglists:[],
    typelist:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
    t.getMessageTypeList()
  },
  onShow(){
    var t = this
    t.setData({
      page:1,
      msglists:[],
    })
    t.getMessageList()

  },
  onReachBottom:function(){
    var t = this
    t.setData({
      page:t.data.page +1
    })
    t.getMessageList()

  },
  gotomessageadd(e){
    var t = this
    wx.navigateTo({
      url:'/pages/message/add/index'
    })
  },
  gotomessageinfo(e){
    var t = this
    wx.navigateTo({
      url:'/pages/message/info/index?id='+e.currentTarget.dataset.id
    })
  },
  // 消息分类列表
  getMessageTypeList(){
    var t = this
    var typelist = wx.getStorageSync('message_type_list')
    if(typelist){
      t.setData({
        typelist:typelist
      })
      return false
    }
    $api.getMessageTypeList().then(res=>{
      wx.setStorageSync('message_type_list', res.data)
      t.setData({
        typelist:res.data
      })
    })
  },
  // 消息列表
  getMessageList(data){
    var t = this,
    data = {
      member_id:wx.getStorageSync('memberinfo').member_id,
      message_type:"all",
      pageNo:t.data.page,
      child_id:"",
    }
    $api.getMessageList(data).then(res=>{
      var msglists = t.data.msglists
      msglists = msglists.concat(res.data)
      t.setData({
        msglists:msglists
      })
    })
   
  },
  gotomessagetype(e){
    var t = this
    wx.navigateTo({
      url: '/pages/message/type/index?dict_key='+e.currentTarget.dataset.key,
    })
  }
})